const express = require("express");
const router = express.Router();
const path = require('path')
const fs = require('fs')
const multer = require('multer')
const upload = multer({ dest: path.join(__dirname, '../../public/img/') });
//加密token
const { sign, verify } = require("../../utils/jwt")

//用户中心管理
router.use("/user", require("./user/userRouter"));

// 员工打卡管理
router.use("/punch", require("./punch/punchRouter"));
// 工资详情
router.use("/salary", require("./salary/salary"));
// 出差申请详情
router.use("/business", require("./business/business"));
// 补卡申请详情
router.use("/replacementCard", require("./replacementCard/replacementCard"));
// 请假申请详情
router.use("/leaveRequest", require("./leaveRequest/leaveRequest"));
// 加班申请详情
router.use("/applicationForExtraWork", require("./applicationForExtraWork/applicationForExtraWork"));

// 员工假期管理
router.use("/vacation", require("./vacation/vacationRouter"));

//公司日志
router.use("/companylog", require("./CompanyLog/companylog"));

//消息公告
router.use('/message', require('./message/message'))

//部门管理
router.use('/department', require('./department/department'))

// 聊天室
router.use('/chat', require('./chat/chat'))

// 验证token是否过期
router.post('/token', (req, res, next) => {
  try {
    // 验证token是否过去
    let tokenVer = verify(req.body.token)
    res.send({
      code: 200,
    })
  } catch (err) {
    console.log('token已经过期了', err);
    res.send({
      code: 400,
      message: 'token已经过期了,请重新登录'
    })
  }
});


// 接收上传的图片，返回url地址
router.post('/imgUrl', upload.single('avatar'), (req, res, next) => {
  let file = req.file;
  if (file) {
    let oldName = file.path;
    let newName = file.destination + file.originalname;
    fs.renameSync(oldName, newName)
    let url = "http://localhost:3000/img/" + file.originalname;
    res.send({
      code: 200,
      url
    })
  } else {
    res.send({
      code: 400,
      message: '未接收图片'
    })
  }
})

module.exports = router;
